#include <xp/bas/string.h>
#include <xp/bas/stdio.h>
#include <xp/bas/locale.h>

int xp_main (int argc, xp_char_t* argv[])
{
	xp_byte_t buf[1000];
	xp_char_t buf2[1000];
	xp_size_t n, n2, i, j;

	xp_setlocale ();

	if (argc != 2) {
		xp_printf (XP_TEXT("usage: %s domain_name\n"), argv[0]);
		return -1;
	}

	n = xp_strtodn (argv[1], buf, xp_countof(buf));
	if (n == 0) {
		xp_printf (XP_TEXT("usage: cannot convert %s\n"), argv[1]);
		return -1;
	}

	for (i = 0; i < n; i += buf[i] + 1) {
		xp_printf (XP_TEXT("%d "), buf[i]);
		for (j = 0; j < buf[i]; j++) {
			xp_printf (XP_TEXT("%c"), buf[i + j + 1]);
		}
		xp_printf (XP_TEXT(" "));
	}
	xp_printf (XP_TEXT("\n"));

	n = xp_strxtodn (argv[1], xp_strlen(argv[1]), buf, xp_countof(buf));
	if (n == 0) {
		xp_printf (XP_TEXT("usage: cannot convert %s\n"), argv[1]);
		return -1;
	}

	for (i = 0; i < n; i += buf[i] + 1) {
		xp_printf (XP_TEXT("%d "), buf[i]);
		for (j = 0; j < buf[i]; j++) {
			xp_printf (XP_TEXT("%c"), buf[i + j + 1]);
		}
		xp_printf (XP_TEXT(" "));
	}
	xp_printf (XP_TEXT("\n"));

	n2 = xp_strfromdn (buf, n, buf2, xp_countof(buf2));
	xp_printf (XP_TEXT("%lu [%s]\n"), (unsigned long)n2, buf2);

	n2 = xp_strxfromdn (buf, n, buf2, xp_countof(buf2));
	buf2[n2] = XP_CHAR('\0');
	xp_printf (XP_TEXT("%lu [%s]\n"), (unsigned long)n2, buf2);

	return 0;
}
